#-*-coding:utf-8-*-
# Create your views here.
from models import Members
from models import Salesperson
from models import Agent
from models import Projects
from models import Project_sales_rate,Project_allocate
import decimal


    #处理注册时自动分配.
def allocation_reg (project_id,username,samecode,emailv,group_id=''):
    #这客户是否同时报名多个PROJECT.如是.则尽量把它分配给之前那项目的同一SALES.
    from django.db.models import Q
    member_list=Members.objects.filter(Q(name=username,samecode=samecode)|Q(email=emailv)).order_by("id")
    hadsale_obj=''
    if member_list.count()>0:
        try:
            #可能之前的没分配SALES的
            hadsale_obj=member_list[0].salesperson
        except:
            pass
        #已入库有一项目,找出之前项目的SALES.
    hadok=''
    project_id = int(project_id)
    sales_id=0#将分配的sales的ID,返回此值
    if hadsale_obj:
        #之前有SALE.如果些SALES也负责此项目,把些客户也分给他.
        psrate_list=Project_sales_rate.objects.filter(project_id=project_id,salesperson_id=hadsale_obj.id,rate__gt=0)
        if psrate_list.count()>0:
            sales_id=hadsale_obj.id
            hadok=1
            try:
                allorate_obj=Project_allocate.objects.get(project_id=project_id,salesperson_id=sales_id)
                allorate_obj.sum=allorate_obj.sum+1
                allorate_obj.save()
            except:
                pass

    if project_id!='' and hadok=='':
        project_id=int(project_id)
        from django.db import connection
        cursor=connection.cursor()
        agent_list="select * from crm_agent"
        cursor.execute(agent_list)
        agentdata = cursor.fetchall()
        agent={}
        for rec in agentdata:
            agent[rec[1]]=rec[0]#得到agent字典
    #-------------------------------------------
        #查出管理员设定的每个sales的比例.
        if group_id!='':
            salerate_list="select * from crm_project_sales_rate where project_id=%s and group_id=%s"%(project_id,group_id)
            cursor.execute(salerate_list)
        else:
            salerate_list="select * from crm_project_sales_rate where project_id=%s"
            cursor.execute(salerate_list,[project_id])
        salesratelist = cursor.fetchall()
        category_dict={}
        averrate=0
        i=0
        for salerate in salesratelist:
            category_dict[salerate[2]]=salerate[3]#对应的sales对应的比例.
            if i==0:
                averrate=salerate[3]
            if salerate[3]<averrate:
                averrate=salerate[3]
            i+=1
        #for auto allocate------------------------------>
        if group_id!='':
            sql4="select * from crm_project_allocate where project_id=%s and group_id=%s"%(project_id,group_id)
            cursor.execute(sql4)
        else:
            sql4="select * from crm_project_allocate where project_id=%s"
            cursor.execute(sql4,[project_id])
        
        salesumlist = cursor.fetchall()
        allsum=0
        for sales in salesumlist:
            allsum+=decimal.Decimal(sales[3])
        bigrate=0
        salessumid=0
        j=0
        temp_dict={}
        for salessum in salesumlist:
            if category_dict.has_key(salessum[2]):
                temp_dict[j]=category_dict[salessum[2]]
                myrate=decimal.Decimal(category_dict[salessum[2]])
                if allsum!=0:
                    nowbigrate=decimal.Decimal(category_dict[salessum[2]])-decimal.Decimal(salessum[3])*100/decimal.Decimal(allsum)
                else:
                    nowbigrate=decimal.Decimal(category_dict[salessum[2]])
            else:
                nowbigrate = 0
                temp_dict[j]=0
                myrate=0
            if j==0:
                bigrate=nowbigrate
                sales_id=salessum[2]
                salessumid=salessum[0]
            if int(nowbigrate)>int(bigrate):
                bigrate=nowbigrate
                sales_id=salessum[2]
                salessumid=salessum[0]
            if int(nowbigrate)==int(bigrate) and j!=0 and int(temp_dict[j-1])==0 and int(temp_dict[j])!=0:
                bigrate=nowbigrate
                sales_id=salessum[2]
                salessumid=salessum[0]
            j+=1
        try:
            allorate_obj=Project_allocate.objects.get(project_id=project_id,salesperson_id=sales_id)
            allorate_obj.sum=allorate_obj.sum+1
            allorate_obj.save()
        except:
            pass
    return sales_id


